package com.livesource.client.webservices.github;

import com.google.gwt.json.client.JSONObject;
import com.livesource.client.model.entry.ProjectClass;
import com.livesource.client.model.entry.ProjectFeature;
import com.livesource.client.utilities.ConvertJson;
import com.livesource.client.utilities.FeatureNameGenerator;
import com.livesource.client.utilities.FileExtensions;

public class ConvertGithubToProjectEntry {

	public static ProjectClass convertJsonp(JSONObject obj) {

		// {"type":"blob", "sha":"228ca4216c8f4dab71340e23594f718471b737fa",
		// "size":395,
		// "url":"https://api.github.com/repos/allineo/TravelLog/git/blobs/228ca4216c8f4dab71340e23594f718471b737fa",
		// "path":".settings/org.eclipse.jdt.core.prefs", "mode":"100644"}

		ProjectClass file = new ProjectClass();

		file.setFilePath(FeatureNameGenerator.getFilePath(
				ConvertJson.convertToString(obj.get("path")), "/"));

		file.setFileName(FeatureNameGenerator.getLastName(
				ConvertJson.convertToString(obj.get("path")), "/"));

		file.setSha(ConvertJson.convertToString(obj.get("sha")));

		file.setFileExtension(FileExtensions.getFileExtension(file
				.getFileName()));

		setEntryFeature(file, file.getFilePath(), file.getFileName());

		return file;
	}

	private static void setEntryFeature(ProjectFeature feature,
			String entryPath, String entryName) {

		feature.setFeaturePath(FeatureNameGenerator.getLastNameSpaced(
				entryPath, "/"));

		feature.setFeatureName(FeatureNameGenerator
				.getFileSpacedName(entryName));
	}
}
